﻿using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.Collections.Generic;

public class MainService : MonoBehaviour {

	public PlayerStorage Main;
	public PlayerVehicle Player;
	
	public GameObject EndgamePopup;
	public List<GameObject> PlayerVehicles;
	
	public Text PopupText;
	public Text PopupDetails;
	
	
	public Animator EnginePanel;
	
	public AudioSource IntroMusic;
	public AudioSource Music;
	public AudioSource OutroMusic;
	public AudioSource EngineIdle;
	public AudioSource EnginneRunning;
	
	public Slider Gear;
	public Button Engine;
	
	public bool gamestart = false;
	public bool introFinished = false;
	public bool gamefinished = false;
	public bool gamePaused = false;
	public bool trueDeath = false;
	
	public delegate void GameManager();

	public static event GameManager GameStart;
	public static event GameManager GameEnd;

	
	void Start()
	{
		Main.LoadValues();
		PickVehicle(Main.activeVehicle);
		Engine.interactable = false;
	;
	
	
	}
	
	void PickVehicle(int x)
	{
		if (Main.activeVehicle == x)
		{
			for (int a =0; a < PlayerVehicles.Count; a++)
			{
				PlayerVehicles[a].SetActive(false);
				
			}
			PlayerVehicles[x].SetActive(true);
			Player = PlayerVehicles[x].GetComponent<PlayerVehicle>();
		}
		
		
	}
	
	public void StartupMusic()
	{
		IntroMusic.Play();
		
	}
	
	
	void StartupGear()
	{
		if (Gear.value == 1)
		{
			EngineIdle.Play();
			Engine.interactable = true;
			StartupMusic();
			gamestart = true;
		}
		
	}
	
	public void StartGame()
	{
	
		
		if (GameStart != null)
		{
			GameStart();
			
		}
		EngineIdle.Stop ();
		EnginneRunning.Play ();
	
	}
	
	public void MoveEnginePanel()
	{
		EnginePanel.SetInteger("Go", 1);
	
	}
	
	public void PauseGame()
	{
		Debug.Log ("Pause");
		gamePaused = true;
		Time.timeScale = 0;
	}
	

	
	void endgamePopup()
	{
		EndgamePopup.SetActive(true);
		stopAllMusic();
		OutroMusic.Play ();

		Main.firstrun = 1;
		
	}
	
	public void stopAllMusic()
	{
		IntroMusic.Stop ();
		Music.Stop ();
		EngineIdle.Stop ();
		EnginneRunning.Stop ();
		
		
	}
	
	void failCondition()
	{
		int diceRoll = Random.Range (0,11);
		if( diceRoll < 7)
		{
			trueDeath = true;
		}
	
	}
	
	
	void Update()
	{
		if (Main.Sound == 1)
		{
			AudioListener.pause = true;
		}
		else
		{
			AudioListener.pause = false;
		}
	
		if(gamestart == true)
		{
				if (introFinished == false)
				{
					
					if(IntroMusic.isPlaying == false)
					{
						
						introFinished = true;
						Music.Play ();
					}
				}
		}
		
		
	
		if (gamestart == false)
		{
			StartupGear();
		
		}
	
		if (gamestart == true)
		{
			if (gamefinished == false)
			{
				
				if (Player.distance <= 0)
				{
					gamefinished = true;
					endgamePopup();
					PopupText.text = "DESTINATION REACHED";
					PopupDetails.text = "You made it without serious harm.";
					PauseGame();
					if (GameEnd != null)
					{
						GameEnd();
						
					}
				}
				
				if (Main.fuel <= 0)
				{
					failCondition();
					gamefinished = true;
					endgamePopup();
					PopupText.text = "FUEL DEPLETED";
					if (trueDeath == true)
					{
						PopupDetails.text = "You've run out of fuel, and are left to perish.";
					}
					if (trueDeath == false)
					{
						PopupDetails.text = "You've somehow managed to make it through despite being out of fuel.";
					}
					PauseGame();
					
					if (GameEnd != null)
					{
						GameEnd();
					}
				}
				if (Main.hull <= 0)
				{
					failCondition();
					gamefinished = true;
					endgamePopup();
					PopupText.text = "TRUKK WRECKED";
					if (trueDeath == true)
					{
						PopupDetails.text = "Your trukk and your life were taken by the road.";
					}
					if (trueDeath == false)
					{
						PopupDetails.text = "Despite being a burning wreck, you made it through.";
					}
					PauseGame();
					if (GameEnd != null)
					{
						GameEnd();
					}
				}
			}
		}
	}
	

}
